package com.exam.commonbiz.widget;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.exam.commonbiz.R;
import com.exam.commonbiz.adapter.BottomSheetDialogAdapter;
import com.exam.commonbiz.bean.BottomSheetDialogInfo;
import com.exam.commonbiz.util.CommonCallBack;
import com.exam.commonbiz.util.OnFastClickListener;
import com.google.android.material.bottomsheet.BottomSheetDialog;

import java.util.List;

public class CommonBottomSheetDialog {

    public void show(Context context, List<BottomSheetDialogInfo> list, CommonCallBack commonCallBack) {
        // 参数2：设置BottomSheetDialog的主题样式；将背景设置为transparent，这样我们写的shape_bottom_sheet_dialog.xml才会起作用
        BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(context, R.style.BottomSheetDialog);
        //不传第二个参数
        //BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(this);

        // 底部弹出的布局
        View bottomView = LayoutInflater.from(context).inflate(R.layout.bottom_sheet_layout, null);
        RecyclerView recyclerView = bottomView.findViewById(R.id.recyclerView);
        BottomSheetDialogAdapter bottomSheetDialogAdapter = new BottomSheetDialogAdapter();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(bottomSheetDialogAdapter);
        bottomSheetDialogAdapter.setNewData(list);
        bottomSheetDialogAdapter.setOnItemClickListener(new BottomSheetDialogAdapter.OnItemClickListener() {

            @Override
            public void onItemClick(BottomSheetDialogInfo messageInfo) {
                bottomSheetDialog.dismiss();
                if (commonCallBack != null) {
                    commonCallBack.callback(messageInfo.position, messageInfo);
                }
            }
        });
        bottomView.findViewById(R.id.tv_cancel).setOnClickListener(new OnFastClickListener() {
            @Override
            public void onFastClick(View v) {
                bottomSheetDialog.dismiss();
            }
        });
        bottomSheetDialog.setContentView(bottomView);
        //设置点击dialog外部不消失
        //bottomSheetDialog.setCanceledOnTouchOutside(false);
        bottomSheetDialog.show();
    }
}
